package com.agileplex.codeminer.commons.props;

import java.util.Hashtable;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.log4j.Logger;

/**
 * Properties include locale information
 * 
 * @creator Weiming Yin
 * @date 2007-5-31
 * 
 * @lastmodified
 * @author $Author: yinweiming $
 * @version $Version$, $Date: 2007-07-16 18:12:59 +0000 (Mon, 16 Jul 2007) $
 */
public class Properties {
	private static Logger logger = Logger.getLogger(Properties.class);

	static final String DEFAULT_ENCODING = "utf8";

	final String encoding;

	protected Hashtable<String, String> props;

	public Properties() {
		this(DEFAULT_ENCODING);
	}

	public Properties(String encoding) {
		this.encoding = encoding;
		props = new Hashtable<String, String>();
	}

	public Properties load(File f) throws IOException {
		if (f.exists() && !f.isDirectory() && f.canRead()) {
			InputStream is = null;
			try {
				is = new FileInputStream(f);
				return load(is);
			} catch (FileNotFoundException e) {
				logger.debug(e);
				throw e;
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						logger.debug(e);
					}
				}
			}
		} else {
			throw new IOException("File error");
		}
	}

	public synchronized Properties load(InputStream is) {
		if (is == null) {
			return this;
		}
		
		InputStreamReader isReader = null;
		BufferedReader bufReader = null;

		isReader = new InputStreamReader(is);
		bufReader = new BufferedReader(isReader);

		try {
			while (bufReader.ready()) {
				String line = bufReader.readLine();
				if (line == null || "".equals(line.trim())) {
					continue;
				}

				try {
					line = new String(line.getBytes(), this.encoding);
				} catch (UnsupportedEncodingException e) {
					logger.debug(e);
				}

				String[] pair = line.split(",");
				if (pair.length == 2) {
					set(pair[0].trim(), pair[1].trim());
				} else {
					set(pair[0].trim(), "");
				}
			}
		} catch (IOException e) {
			logger.debug(e);
		}
		
		return this;
	}

	public String get(String key) {
		return props.get(key);
	}

	public String set(String key, String value) {
		return props.put(key, value);
	}

	public void save(File f) {

	}

	public synchronized void save(OutputStream os) {

	}
	
	public static Properties loadProperties(InputStream is) throws IOException {
		return loadProperties(is, DEFAULT_ENCODING);
	}
	
	public static Properties loadProperties(InputStream is, String encoding) throws IOException {
		Properties prop = new Properties(encoding);
		return prop.load(is);
	}

	public static Properties loadProperties(File f) throws IOException {
		return loadProperties(f, DEFAULT_ENCODING);
	}

	public static Properties loadProperties(File f, String encoding) throws IOException {
		Properties prop = new Properties(encoding);
		return prop.load(f);
	}
}
